home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / library / reqtlsdv.lha / ReqTools / demo / demo.c < prev    next >
C/C++ Source or Header  |  1994-01-02  |  14KB  |  447 lines

  1. /*********************************
  2. *                                *
  3. *  reqtools.library (V38)        *
  4. *                                *
  5. *  Release 2.2                   *
  6. *                                *
  7. *  (c) 1991-1994 Nico François   *
  8. *                                *
  9. *  demo.c                        *
  10. *                                *
  11. *  This source is public domain  *
  12. *  in all respects.              *
  13. *                                *
  14. *********************************/
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <exec/execbase.h>
  19. #include <libraries/dos.h>
  20. #include <intuition/intuition.h>
  21. #include <proto/exec.h>
  22. #include <proto/dos.h>
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. #include <libraries/reqtools.h>
  29. #include <proto/reqtools.h>
  30.  
  31. extern struct ExecBase *SysBase;
  32. struct ReqToolsBase *ReqToolsBase;
  33.  
  34. #define REG register
  35.  
  36. /* Callback functions
  37.     Define DOHOOKS when you compile to activate these filter functions */
  38.  
  39. BOOL __asm __saveds file_filterfunc (
  40.     REG __a0 struct Hook *, REG __a2 struct rtFileRequester *,
  41.     REG __a1 struct FileInfoBlock *
  42.     );
  43. BOOL __asm __saveds font_filterfunc (
  44.     REG __a0 struct Hook *, REG __a2 struct rtFontRequester *,
  45.     REG __a1 struct TextAttr *
  46.     );
  47. BOOL __asm __saveds vol_filterfunc (
  48.     REG __a0 struct Hook *, REG __a2 struct rtFileRequester *,
  49.     REG __a1 struct rtVolumeEntry *
  50.     );
  51.  
  52. ULONG undertag[] = { RT_Underscore, '_', TAG_END };
  53.  
  54. void myputs (char *str)
  55. {
  56.     Write (Output(), str, strlen(str));
  57. }
  58.  
  59. void __stdargs __main (char *line)
  60. {
  61.     struct rtFileRequester *filereq;
  62.     struct rtFontRequester *fontreq;
  63.     struct rtScreenModeRequester *scrmodereq;
  64.     struct rtFileList *flist, *tempflist;
  65.     struct Hook filterhook, font_filterhook, vol_filterhook;
  66.     char buffer[128], filename[34];
  67.     ULONG longnum, ret, color;
  68.  
  69.     if (!(ReqToolsBase = (struct ReqToolsBase *)
  70.                                           OpenLibrary (REQTOOLSNAME, REQTOOLSVERSION))) {
  71.         myputs ("You need reqtools.library V38 or higher!\n"
  72.                   "Please install it in your Libs: drirectory.\n");
  73.         exit (0);
  74.         }
  75.  
  76.     rtEZRequest ("ReqTools 2.0 Demo\n"
  77.                  "~~~~~~~~~~~~~~~~~\n"
  78.                      "'reqtools.library' offers several\n"
  79.                      "different types of requesters:",
  80.                      "Let's see them", NULL, NULL);
  81.  
  82.     rtEZRequest ("NUMBER 1:\nThe larch :-)", "Be serious!", NULL, NULL);
  83.  
  84.     rtEZRequest ("NUMBER 1:\nString requester\nfunction: rtGetString()",
  85.                      "Show me", NULL, NULL);
  86.  
  87.     strcpy (buffer, "A bit of text");
  88.     if (!rtGetString (buffer, 127, "Enter anything:", NULL, TAG_END))
  89.         rtEZRequest ("You entered nothing :-(", "I'm sorry", NULL, NULL);
  90.     else
  91.         rtEZRequest ("You entered this string:\n'%s'.",
  92.                          "So I did", NULL, NULL, buffer);
  93.  
  94.     ret = rtGetString (buffer, 127, "Enter anything:", NULL,
  95.                              RTGS_GadFmt, " _Ok |New _2.0 feature!|_Cancel",
  96.                              RTGS_TextFmt,
  97.                                  "These are two new features of ReqTools 2.0:\n"
  98.                                   "Text above the entry gadget and more than\n"
  99.                                   "one response gadget.",
  100.                      TAG_MORE, undertag);
  101.     if (ret == 2)
  102.         rtEZRequest ("Yep, this is a new\nReqTools 2.0 feature!",
  103.                          "Oh boy!", NULL, NULL);
  104.  
  105.     ret = rtGetString (buffer, 127, "Enter anything:", NULL,
  106.                              RTGS_GadFmt, " _Ok |_Abort|_Cancel",
  107.                              RTGS_TextFmt,
  108.                                  "New is also the ability to switch off the\n"
  109.                                   "backfill pattern.  You can also center the\n"
  110.                                   "text above the entry gadget.\n"
  111.                                   "These new features are also available in\n"
  112.                                   "the rtGetLong() requester.",
  113.                              RTGS_BackFill, FALSE,
  114.                              RTGS_Flags, GSREQF_CENTERTEXT|GSREQF_HIGHLIGHTTEXT,
  115.                              TAG_MORE, undertag);
  116.     if (ret == 2)
  117.         rtEZRequest ("What!! You pressed abort!?!\nYou must be joking :-)",
  118.                          "Ok, Continue", NULL, NULL);
  119.                          
  120.  
  121.     rtEZRequest ("NUMBER 2:\nNumber requester\nfunction: rtGetLong()",
  122.                      "Show me", NULL, NULL);
  123.  
  124.     if (!rtGetLong (&longnum, "Enter a number:", NULL,
  125.                          RTGL_ShowDefault, FALSE,
  126.                          RTGL_Min, 0, RTGL_Max, 666,TAG_END))
  127.         rtEZRequest ("You entered nothing :-(",
  128.                          "I'm sorry", NULL, NULL);
  129.     else
  130.         rtEZRequest ("The number you entered was:\n%ld%s",
  131.                          "So it was", NULL, NULL, longnum,
  132.                             (longnum == 666) ? " (you devil! :)" : "");
  133.  
  134.     rtEZRequest ("NUMBER 3:\nMessage requester, the requester\n"
  135.                      "you've been using all the time!\nfunction: rtEZRequest()",
  136.                      "Show me more", NULL, NULL);
  137.  
  138.     rtEZRequest ("Simplest usage: some body text and\na single centered gadget.",
  139.                      "Got it", NULL, NULL);
  140.  
  141.     while (!rtEZRequest ("You can also use two gadgets to\n"
  142.                                 "ask the user something.\n"
  143.                                 "Do you understand?", "Of course|Not really",
  144.                                 NULL, NULL))
  145.         rtEZRequest ("You are not one of the brightest are you?\n"
  146.                          "We'll try again...",
  147.                          "Ok", NULL, NULL);
  148.  
  149.     rtEZRequest ("Great, we'll continue then.", "Fine", NULL, NULL);
  150.  
  151.     switch (rtEZRequest ("You can also put up a requester with\n"
  152.                                 "three choices.\n"
  153.                                 "How do you like the demo so far ?",
  154.                                 "Great|So so|Rubbish", NULL, NULL)) {
  155.         case FALSE:
  156.             rtEZRequest ("Too bad, I really hoped you\nwould like it better.",
  157.                              "So what", NULL, NULL);
  158.             break;
  159.         case TRUE:
  160.             rtEZRequest ("I'm glad you like it so much.", "Fine", NULL, NULL);
  161.             break;
  162.         case 2:
  163.             rtEZRequest ("Maybe if you run the demo again\n"
  164.                              "you'll REALLY like it.",
  165.                              "Perhaps", NULL, NULL);
  166.             break;
  167.         }
  168.  
  169.     ret = rtEZRequestTags ("The number of responses is not limited to three\n"
  170.                                   "as you can see.  The gadgets are labeled with\n"    
  171.                                   "the return code from rtEZRequest().\n"
  172.                                   "Pressing Return will choose 4, note that\n"
  173.                                   "4's button text is printed in boldface.",
  174.                                   "1|2|3|4|5|0", NULL, NULL,
  175.                                   RTEZ_DefaultResponse, 4, TAG_END);
  176.     rtEZRequest ("You picked '%ld'.", "How true", NULL, NULL, ret);
  177.  
  178.     rtEZRequestTags ("New for Release 2.0 of ReqTools (V38) is\n"
  179.                           "the possibility to define characters in the\n"
  180.                           "buttons as keyboard shortcuts.\n"
  181.                           "As you can see these characters are underlined.\n"
  182.                           "Pressing shift while still holding down the key\n"
  183.                           "will cancel the shortcut.\n"
  184.                           "Note that in other requesters a string gadget may\n"
  185.                           "be active.  To use the keyboard shortcuts there\n"
  186.                           "you have to keep the Right Amiga key pressed down.",
  187.                           "_Great|_Fantastic|_Swell|Oh _Boy",
  188.                           NULL, NULL,
  189.                           RT_Underscore, '_', TAG_END);
  190.  
  191.     rtEZRequest (
  192.         "You may also use C-style formatting codes in the body text.\n"
  193.         "Like this:\n\n"
  194.         "'The number %%ld is written %%s.' will give:\n\n"
  195.         "The number %ld is written %s.\n\n"
  196.         "if you also pass '5' and '\"five\"' to rtEZRequest().",
  197.         "_Proceed", NULL, (struct TagItem *)&undertag, 5, "five");
  198.  
  199.     if (rtEZRequestTags ("It is also possible to pass extra IDCMP flags\n"
  200.                                "that will satisfy rtEZRequest(). This requester\n"
  201.                                "has had DISKINSERTED passed to it.\n"
  202.                                "(Try inserting a disk).",
  203.                                "_Continue", NULL, NULL,
  204.                                RT_IDCMPFlags, DISKINSERTED,
  205.                                 RT_Underscore, '_', TAG_END)
  206.                                & DISKINSERTED)
  207.         rtEZRequest ("You inserted a disk.", "I did", NULL, NULL);
  208.     else
  209.         rtEZRequest ("You used the 'Continue' gadget\n"
  210.                          "to satisfy the requester.", "I did", NULL, NULL);
  211.  
  212.     rtEZRequestTags ("Finally, it is possible to specify the position\n"
  213.                           "of the requester.\n"
  214.                           "E.g. at the top left of the screen, like this.\n"
  215.                           "This works for all requesters, not just rtEZRequest()!",
  216.                           "_Amazing", NULL, NULL,
  217.                           RT_ReqPos, REQPOS_TOPLEFTSCR,
  218.                           RT_Underscore, '_', TAG_END);
  219.  
  220.     rtEZRequestTags ("Alternatively, you can center the\n"
  221.                           "requester on the screen.\n"
  222.                           "Check out 'reqtools.doc' for all the possibilities.",
  223.                           "I'll do that", NULL, NULL,
  224.                           RT_ReqPos, REQPOS_CENTERSCR, TAG_END);
  225.  
  226.     rtEZRequestTags ("NUMBER 4:\nFile requester\n"
  227.                           "function: rtFileRequest()",
  228.                           "_Demonstrate", NULL, NULL, RT_Underscore, '_', TAG_END);
  229.  
  230.     if (filereq = rtAllocRequestA (RT_FILEREQ, NULL)) {
  231.  
  232.         filterhook.h_Entry = (ULONG (*)())file_filterfunc;
  233.  
  234.         filename[0] = 0;
  235.         if (rtFileRequest (filereq, filename, "Pick a file",
  236. #ifdef DOHOOKS
  237.                                  RTFI_FilterFunc, &filterhook,
  238. #endif
  239.                                  TAG_END))
  240.             rtEZRequest ("You picked the file:\n'%s'\nin directory:\n'%s'",
  241.                              "Right", NULL, NULL, filename, filereq->Dir);
  242.         else
  243.             rtEZRequest ("You didn't pick a file.", "No", NULL, NULL);
  244.  
  245.         rtEZRequestTags ("The file requester has the ability\n"
  246.                               "to allow you to pick more than one\n"
  247.                               "file (use Shift to extend-select).\n"
  248.                               "Note the extra gadgets you get.",
  249.                               "_Interesting", NULL, NULL, RT_Underscore, '_', TAG_END);
  250.  
  251.         flist = rtFileRequest (filereq, filename, "Pick some files",
  252.                                       RTFI_Flags, FREQF_MULTISELECT, TAG_END);
  253.         if (flist) {
  254.             rtEZRequest ("You selected some files, this is\n"
  255.                              "the first one:\n"
  256.                              "'%s'\n"
  257.                              "All the files are returned as a linked\n"
  258.                              "list (see demo.c and reqtools.h).",
  259.                              "Aha", NULL, NULL, flist->Name);
  260.             /* Traverse all selected files */
  261.             /*
  262.             tempflist = flist;
  263.             while (tempflist) {
  264.                 DoSomething (tempflist->Name, tempflist->StrLen);
  265.                 tempflist = tempflist->Next;
  266.                 }
  267.             */
  268.             /* Free filelist when no longer needed! */
  269.             rtFreeFileList (flist);
  270.             }
  271.  
  272.         rtFreeRequest (filereq);
  273.         }
  274.     else
  275.         rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  276.  
  277.  
  278.     rtEZRequestTags ("The file requester can be used\n"
  279.                           "as a directory requester as well.",
  280.                           "Let's _see that", NULL, NULL, RT_Underscore, '_', TAG_END);
  281.  
  282.     if (filereq = rtAllocRequestA (RT_FILEREQ, NULL)) {
  283.         if (rtFileRequest (filereq, filename, "Pick a directory",
  284.                                  RTFI_Flags, FREQF_NOFILES, TAG_END))
  285.             rtEZRequest ("You picked the directory:\n'%s'",
  286.                              "Right", NULL, NULL, filereq->Dir);
  287.         else
  288.             rtEZRequest ("You didn't pick a directory.", "No", NULL, NULL);
  289.  
  290.         rtFreeRequest (filereq);
  291.         }
  292.     else
  293.         rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  294.  
  295.     rtEZRequest ("NUMBER 5:\nFont requester\n"
  296.                      "function: rtFontRequest()",
  297.                      "Show", NULL, NULL);
  298.  
  299.     if (fontreq = rtAllocRequestA (RT_FONTREQ, NULL)) {
  300.  
  301.         fontreq->Flags = FREQF_STYLE|FREQF_COLORFONTS;
  302.         font_filterhook.h_Entry = (ULONG (*)())font_filterfunc;
  303.  
  304.         if (rtFontRequest (fontreq, "Pick a font",
  305. #ifdef DOHOOKS
  306.                                  RTFO_FilterFunc, &font_filterhook,
  307. #endif
  308.                                  TAG_END))
  309.             rtEZRequest ("You picked the font:\n'%s'\nwith size:\n'%ld'",
  310.                              "Right", NULL, NULL,
  311.                              fontreq->Attr.ta_Name, fontreq->Attr.ta_YSize);
  312.         else
  313.             rtEZRequestTags ("You canceled.\nWas there no font you liked ?",
  314.                                   "_Nope", NULL, NULL, RT_Underscore, '_', TAG_END);
  315.  
  316.         rtFreeRequest (fontreq);
  317.         }
  318.     else
  319.         rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  320.  
  321.     rtEZRequestTags ("NUMBER 6:\nPalette requester\n"
  322.                           "function: rtPaletteRequest()",
  323.                           "_Proceed", NULL, NULL, RT_Underscore, '_', TAG_END);
  324.  
  325.     color = rtPaletteRequest ("Change palette", NULL, TAG_END);
  326.     if (color == -1)
  327.         rtEZRequest ("You canceled.\nNo nice colors to be picked ?",
  328.                          "Nah", NULL, NULL);
  329.     else
  330.         rtEZRequest ("You picked color number %ld.", "Sure did",
  331.                          NULL, NULL, color);
  332.  
  333.     rtEZRequestTags ("NUMBER 7: (ReqTools 2.0)\n"
  334.                           "Volume requester\n"
  335.                           "function: rtFileRequest() with\n"
  336.                           "          RTFI_VolumeRequest tag.",
  337.                           "_Show me", NULL, NULL, RT_Underscore, '_', TAG_END);
  338.  
  339.     if (filereq = rtAllocRequestA (RT_FILEREQ, NULL)) {
  340.  
  341.         vol_filterhook.h_Entry = (ULONG (*)())vol_filterfunc;
  342.  
  343.         if (rtFileRequest (filereq, NULL, "Pick a volume",
  344. #ifdef DOHOOKS
  345.                                  RTFI_FilterFunc, &vol_filterhook,
  346. #endif
  347.                                  RTFI_VolumeRequest, 0, TAG_END))
  348.             rtEZRequest ("You picked the volume:\n'%s'",
  349.                              "Right", NULL, NULL, filereq->Dir);
  350.         else
  351.             rtEZRequest ("You didn't pick a volume.", "I did not", NULL, NULL);
  352.  
  353.         rtFreeRequest (filereq);
  354.         }
  355.     else
  356.         rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  357.  
  358.     rtEZRequestTags ("NUMBER 8: (ReqTools 2.0)\n"
  359.                           "Screen mode requester\n"
  360.                           "function: rtScreenModeRequest()\n"
  361.                           "Only available on Kickstart 2.0!",
  362.                           "_Proceed", NULL, NULL, RT_Underscore, '_', TAG_END);
  363.  
  364.     if (SysBase->LibNode.lib_Version < 37)
  365.         rtEZRequestTags ("Your Amiga doesn't seem to have\n"
  366.                               "Kickstart 2.0 in ROM so I am not\n"
  367.                               "able to show you the Screen mode\n"
  368.                               "requester.\n"
  369.                               "So upgrade to 2.0 *now* :-)",
  370.                               "_Allright", NULL, NULL, RT_Underscore, '_', TAG_END);
  371.     else {
  372.         if (scrmodereq = rtAllocRequestA (RT_SCREENMODEREQ, NULL)) {
  373.  
  374.             if (rtScreenModeRequest (scrmodereq, "Pick a screen mode:",
  375.                                              RTSC_Flags, SCREQF_DEPTHGAD|SCREQF_SIZEGADS|
  376.                                                  SCREQF_AUTOSCROLLGAD|SCREQF_OVERSCANGAD,
  377.                                               TAG_END))
  378.                 rtEZRequest ("You picked this mode:\n"
  379.                                  "ModeID  : 0x%lx\n"
  380.                                  "Size    : %ld x %ld\n"
  381.                                  "Depth   : %ld\n"
  382.                                  "Overscan: %ld\n"
  383.                                  "AutoScroll %s",
  384.                                  "Right", NULL, NULL,
  385.                                  scrmodereq->DisplayID,
  386.                                  scrmodereq->DisplayWidth,
  387.                                  scrmodereq->DisplayHeight,
  388.                                  scrmodereq->DisplayDepth,
  389.                                  scrmodereq->OverscanType,
  390.                                  scrmodereq->AutoScroll ? "On" : "Off");
  391.             else
  392.                 rtEZRequest ("You didn't pick a screen mode.", "Nope", NULL, NULL);
  393.  
  394.             rtFreeRequest (scrmodereq);
  395.             }
  396.         else
  397.             rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  398.         }
  399.  
  400.     rtEZRequestTags ("That's it!\n"
  401.                           "Hope you enjoyed the demo", "_Sure did", NULL, NULL,
  402.                           RT_Underscore, '_', TAG_END);
  403.  
  404.     /* free all resources */
  405.     CloseLibrary ((struct Library *)ReqToolsBase);
  406.  
  407.     exit (0);
  408. }
  409.  
  410. /********
  411. * HOOKS *
  412. ********/
  413.  
  414. BOOL __asm __saveds file_filterfunc (
  415.     REG __a0 struct Hook *hook,
  416.     REG __a2 struct rtFileRequester *filereq,
  417.     REG __a1 struct FileInfoBlock *fib
  418.     )
  419. {
  420.     myputs (fib->fib_FileName);
  421.     myputs ("\n");
  422.     return (TRUE);
  423. }
  424.  
  425. BOOL __asm __saveds font_filterfunc (
  426.     REG __a0 struct Hook *hook,
  427.     REG __a2 struct rtFontRequester *fontreq,
  428.     REG __a1 struct TextAttr *textattr
  429.     )
  430. {
  431.     myputs (textattr->ta_Name);
  432.     myputs ("\n");
  433.     return (TRUE);
  434. }
  435.  
  436. BOOL __asm __saveds vol_filterfunc (
  437.     REG __a0 struct Hook *hook,
  438.     REG __a2 struct rtFileRequester *filereq,
  439.     REG __a1 struct rtVolumeEntry *volentry
  440.     )
  441. {
  442.     myputs (volentry->Type == DLT_DEVICE ? "(Volume) " : "(Assign) ");
  443.     myputs (volentry->Name);
  444.     myputs ("\n");
  445.     return (TRUE);
  446. }
  447.